﻿Imports System.Xml
Public Class Form1

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

    End Sub

    Private Sub CollectionBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Initialize the controls and the form.
        Label1.Text = "File Path"
        'Label1.(8, 8, 50, 20)
        TextBox1.Text = "\PFAMobile\champs.xml"
        'TextBox1.SetBounds(64, 8, 256, 20)
        Button1.Text = "Populate TreeView"
        'Button1.SetBounds(8, 40, 200, 20)
        Me.Text = "TreeView control from XML"
        Me.Width = 336
        Me.Height = 368
        'TreeView1.SetBounds(8, 72, 312, 264)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlDoc As New XmlDocument
        xmlDoc.Load(TextBox1.Text)
        Dim racineXML As XmlNode
        Dim racineNode As New TreeNode()
        Dim i As Long
        i = 0
        TreeView1.Nodes.Clear()
        racineXML = xmlDoc.DocumentElement
        'MsgBox(racineXML.Name)
        If racineXML.Name.ToString.Equals("Champs") Then
            racineNode.Text = racineXML.Name
            For Each ferme As XmlNode In racineXML.ChildNodes
                Dim fNode As New TreeNode()
                For Each secteur As XmlNode In ferme.ChildNodes
                    If TypeOf secteur Is XmlText Then
                        fNode.Text = secteur.Value 'Il s'agit du nom de la ferme
                    Else
                        Dim scNode As New TreeNode()
                        For Each serre As XmlNode In secteur.ChildNodes
                            If TypeOf serre Is XmlText Then
                                scNode.Text = serre.Value 'Il s'agit là du nom du secteur
                            Else
                                Dim srNode As New TreeNode()
                                For Each billon As XmlNode In serre.ChildNodes
                                    If TypeOf billon Is XmlText Then
                                        srNode.Text = billon.Value 'Nom de la serre
                                    Else
                                        Dim bNode As New TreeNode()
                                        bNode.Text = billon.FirstChild.Value 'Nom du billon
                                        srNode.Nodes.Add(bNode)
                                    End If
                                Next
                                scNode.Nodes.Add(srNode)
                            End If
                        Next
                        fNode.Nodes.Add(scNode)
                    End If
                Next
                racineNode.Nodes.Add(fNode)
            Next
            TreeView1.Nodes.Add(racineNode)
        Else
            MsgBox("nopp")
        End If
    End Sub


End Class
